<?php
/**
 * User:chendenan
 * Created On 2022/3/13 0013 20:53
 */

declare(strict_types=1);
namespace app\api\controller;

use app\BaseController;

class Login extends BaseController{
    public function index():object{
        if(!$this->request->isPost()){
            return show("非法请求",config("status.error"));
        }
        //$phoneNumber = $this->request->param("phone_number","","trim");
        $name = $this->request->param("name","","trim");
        $code = input("param.code",0,"intval");
        $type = input("param.type",0,"intval");
        //参数校验
        $data = [
            'name' => $name,
            'code' => $code,
            'type' => $type,
        ];
        $validate = new \app\api\validate\User();
        if(!$validate->scene("login")->check($data)){
            return show($validate->getError(),config("status.error"));
        }
        try{
            $result = (new \app\common\business\User())->login($data);
        }catch (\Exception $e){
            return show($e->getMessage(),$e->getCode());
        }

        if($result){
            return show("登录成功",config("status.success"),$result);
        }
        return show("登录失败",config("status.error"));
    }
}